package com.kpiJavaTraining.bogdan.module2;

import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

/**
 * Class loads a TestStack from the temporary file
 * @author Bogdan Olexandr
 */
public class Loader {
	
	private InputStream stream;
	private ObjectInputStream data;
	private TestStack stack;
	private File file;
	
	/**
	 * method loads a TestStack from the temporary file and removes the temporary file
	 * @param user
	 * @return TestStack
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public TestStack load(User user) throws IOException, ClassNotFoundException{
		file = new File(user.toString()+".ucl");
		stream = new FileInputStream(file);
		data = new ObjectInputStream(stream);
		stack = (TestStack)data.readObject();
		data.close();
		file.delete();
		return stack;
	}
}
